package com.dailyblue.java.spring.mvc.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
// 拦截器
1. 实现 HandlerInterceptor 接口
2. 重写它的三个方法
3. 注册这个拦截器
 */
@Component
public class FirstInterceptor implements HandlerInterceptor {
    // 请求之前的拦截
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("FirstInterceptor的preHandle执行了");
        return true;
    }

    // 请求之后的拦截
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("FirstInterceptor的postHandle执行了");
    }

    // 最终的操作
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("FirstInterceptor的afterCompletion执行了");
    }
}
